<template>
  <!-- 预约患者 -->
  <section>
    <el-form :inline="true" @submit.native.prevent="getData(-1)">
      <el-form-item>
        <deptSearch v-model="form.deptId" @change="onChangeDept"></deptSearch>
      </el-form-item>
      <el-form-item>
        <doctorSearch ref="doctorSearch" v-model="form.doctorId" :deptId="form.deptId"></doctorSearch>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.registerType" placeholder="预约类型" clearable>
          <el-option v-for="(item,index) in reserveTypeArray" :key="index" :value="item.id" :label="item.desc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker clearable  v-model="form.dateYY" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="预约日期" end-placeholder="预约日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input v-model="form.nameOrMobile" placeholder="患者姓名或手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.status" clearable placeholder="预约状态">
          <el-option v-for="(item,index) in statusArray" :key="index" v-show="item.id!=0" :value="item.id" :label="item.desc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit" circle icon="el-icon-search"></el-button>
      </el-form-item>
      <!-- <el-form-item>
        <el-button v-if="qxBtns.excel"><i class="el-icon-download"></i>导出</el-button>
      </el-form-item> -->
    </el-form>
    <el-row :gutter="20">
      <el-col v-for="(item,index) in tableList" :key="index" :xs="12" :sm="12" :md="12" :lg="8" :xl="6" class="reserve-user-list">
        <div :class="['card-box',classToName(item.status)]">
          <div class="card-nr">
            <div>
              <img v-if="!item.patientInfo.head" src="@/img/unknown-avatar.png" class="head-img" alt="" style="width:50px;height:50px;">
              <img v-else :src="item.patientInfo.head|imageUrl" class="head-img" alt="" style="width:50px;height:50px;">
            </div>
            <div style="overflow: hidden;">
              <p class="overflow-omit">
                <span style="font-size:18px;">{{item.patientInfo.name}}</span>
                &nbsp;
                <span>({{item.patientInfo.sex|toSex}}&nbsp;{{item.patientInfo.age}}岁)</span>
              </p>
              <div class="overflow-omit">创建时间：{{item.gmtCreate}}</div>
              <div class="overflow-omit">预约：({{item.deptName}}、{{item.registerName}})</div>
              <div class="overflow-omit">预约时间：{{item.dateDay}}&nbsp;{{item.startTime}}~{{item.endTime}}</div>
              <div class="overflow-omit">预约内容：{{item.registerContent}}</div>
            </div>
          </div>
          <div class="card-footer">
            <div @click="onToDetail(item)">查看</div>
            <!-- <div @click="onArrive(item)">到诊</div> -->
            <div v-if="item.status==1" @click="onCancel(item)">取消预约</div>
            <div v-if="item.status==1" @click="onUpdateContent(item)">修改</div>

          </div>
          <div class="zt-box">
            <span v-if="item.status==1" class="success-color">{{item.status|changeAssist(statusArray)}}</span>
            <span v-else-if="item.status==2" class="primary-color">{{item.status|changeAssist(statusArray)}}</span>
            <span v-else-if="item.status==4" class="error-color">{{item.status|changeAssist(statusArray)}}</span>
            <span v-else>{{item.status|changeAssist(statusArray)}}</span>
          </div>
          <div v-if="item.patientInfo.levelId" class="vip-box">
            <el-tooltip :content="item.patientInfo.levelName" placement="top">
              <img src="@/img/icon-vip.png" alt="">
            </el-tooltip>
          </div>
        </div>
      </el-col>
    </el-row>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br>
    <pager v-model="pager" @change-pagenum="getData"></pager>
  </section>
</template>
<script>
export default {
  props: ["qxBtns", "tabPar"],
  components: {
    deptSearch: () => import("@/pages/_com/deptSearch.vue"),
    doctorSearch: () => import("@/pages/_com/doctorSearch.vue")
  },
  data() {
    return {
      form: {
        deptId: "",
        doctorId: "",
        registerType: "",
        nameOrMobile: "",
        medicalCard: "",
        triageStatus: "",
        status: "",
        type: "",
        dateDayStart: "",
        dateDayEnd: "",
        dateYY: [] //预约时间
      },
      reserveTypeArray: [], //预约类型
      statusArray: [], //状态
      pager: { pageSize: 12, pageNum: 1, total: 0 },
      tableList: []
    };
  },
  async mounted() {
    let th = this;
    let date = new Date();
    let newDate = th.$utils.FormatTime("yyyy-MM-dd",date.getTime());
    let lastDate = date.setDate(date.getDate()-6);
    lastDate = new Date(lastDate);
    let oldDate = th.$utils.FormatTime("yyyy-MM-dd",lastDate.getTime());
    th.form.dateYY.push(oldDate,newDate);
    th.showLoading();
    th.reserveTypeArray = await th.getCommonConfigByKey("预约类型");
    th.statusArray = await th.getCommonConfigByKey("预约状态");
    th.getData();
  },
  methods: {
    //查看患者详情
    onToDetail(obj) {
      let th = this;
      if (th.verifyUrl("/page/patientList")) {
        let url = "/page/userDetail";
        let j = th.getUrlToken({
          id: th.jiaMi(obj.patientInfo.patientId)
        });
        th.addNavMenu("病人库", url + th.$utils.JsonToUrl(j), "userDetail");
        th.$router.push({
          path: url,
          query: j
        });
      }
    },
    //修改预约内容
    onUpdateContent(obj) {
      let th = this;
      this.$prompt("修改预约内容", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^.+$/,
        inputValue: obj.registerContent,
        inputErrorMessage: "请输入内容"
      })
        .then(({ value }) => {
          let j = {
            id: obj.id,
            registerContent: value
          };
          th.showLoading(true);
          th.$api
            .updateRegisterRecord(j)
            .then(res => {
              th.showLoading(false);
              if (res.executed) {
                th.$message.success("修改完成");
                obj.registerContent = value;
              } else {
                th.$message.error(res.message);
              }
            })
            .catch(res => {
              th.showLoading(false);
            });
        })
        .catch(() => {});
    },
    //取消预约
    onCancel(obj) {
      let th = this;
      th.$confirm(`确定取消吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          let j = {
            id: obj.id,
            cancelDesc: "手动取消"
          };
          th.showLoading();
          th.$api
            .registerCancelOrder(j)
            .then(res => {
              th.showLoading(false);
              if (res.executed) {
                th.$message.success("取消完成");
                obj.status = 4;
              } else {
                th.$message.error(res.message);
              }
            })
            .catch(res => {
              th.showLoading(false);
            });
        })
        .catch(() => {});
    },
    onChangeDept() {
      let th = this;
      th.form.doctorId = "";
      th.$nextTick(function() {
        th.$refs["doctorSearch"].getData();
      });
    }, 
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      th.showLoading();
      let j = {};
      Object.assign(j, th.form, th.pager);
      delete j.dateYY;
      th.$api.getRegisterListInfo(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
        th.showLoading(false);
      });
    },
    //判断类名
    classToName(type) {
      switch (type) {
        case 1:
          return "bg-yjz";
          break;
        case 2:
          return "bg-sy";
          break;
        case 3:
          return "bg-hong";
          break;
        case 4:
          return "bg-yqx";
          break;
        case 5:
          return "bg-jzz";
          break;
        default:
          return "bg-dfz";
          break;
      }
    }
  },
  watch: {
    "form.dateYY"(n) {
      let th = this;
      th.form.dateDayStart="";
      th.form.dateDayEnd="";
      if(!n) return;
      if (n.length == 0) return;
      th.form.dateDayStart = n[0];
      th.form.dateDayEnd = n[1];
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../_css/reserve.scss";
</style>